#include "saleswindow.h"
#include "ui_saleswindow.h"
#include<QFile>
#include<QDebug>
#include<QDate>
#include<QListWidgetItem>
#include "showsalesinfoform.h"
#include<QTime>
#include"goodsoperatewindow.h"

salesWindow::salesWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::salesWindow)
{
    ui->setupUi(this);
    timer=new QTimer;
    connect(timer,&QTimer::timeout,this,&salesWindow::changeTime);
    timer->start();

    QFile *data=new QFile("./data/saleLog.txt");

    if(!data->open(QIODevice::ReadOnly))
    {
        qDebug()<<"open file failed";
    }

    QListWidgetItem *aitem1=new QListWidgetItem;
    aitem1->setSizeHint(QSize(610,40));
    ui->listWidget->addItem(aitem1);
    showSalesInfoForm *Form1=new showSalesInfoForm;
    ui->listWidget->setItemWidget(aitem1,Form1);
    ui->dateLabel->setText(QDate::currentDate().toString("yy-MM-dd"));

    while (!data->atEnd())
    {
        QByteArray line = data->readLine();
        QListWidgetItem *aitem2=new QListWidgetItem;
        aitem2->setSizeHint(QSize(610,40));
        ui->listWidget->addItem(aitem2);
        totalSale+=line.split('|')[3].toDouble();
        totalProfit+=(line.split('|')[3].toDouble()-line.split('|')[5].toDouble());
         showSalesInfoForm *Form2=new showSalesInfoForm(line.split('|')[0],line.split('|')[1],
         line.split('|')[2],line.split('|')[3].toDouble(),line.split('|')[4],line.split('|')[5].toDouble(),
         line.split('|')[6],line.split('|')[7].toInt(),this);

        ui->listWidget->setItemWidget(aitem2,Form2);



    }
    ui->totalSale->setNum(totalSale);
    ui->ProfitLabel->setNum(totalProfit);
    data->close();
}

salesWindow::~salesWindow()
{
    delete ui;
}

void salesWindow::changeTime()
{
    ui->timeLabel->setText(QTime::currentTime().toString());
}

void salesWindow::on_returnButton_clicked()
{
    QObject* before=this->parentWidget();
    goodsOperateWindow* p=dynamic_cast<goodsOperateWindow*>(before);
    p->show();
    this->close();
}
